package com.java2007.service.impl;

import com.java2007.mapper.BookInfoMapper;
import com.java2007.mapper.ClassInfoMapper;
import com.java2007.pojo.BookInfo;
import com.java2007.pojo.ClassInfo;
import com.java2007.service.BookInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zy
 * @version V1.0
 * @date 2021/03/04
 */
@Service
@Transactional
public class BookInfoServiceImpl implements BookInfoService {

    @Autowired
    private BookInfoMapper bookInfoMapper;

    @Autowired
    private ClassInfoMapper classInfoMapper;

    @Override
    public List<BookInfo> findAll() {
        return bookInfoMapper.findAll();
    }

    @Override
    public BookInfo findClassInfoAndBookInfoByBookInfoId(Long bookInfoId) {
        return bookInfoMapper.findClassInfoAndBookInfoByBookInfoId(bookInfoId);
    }

    @Override
    public int deleteByBookInfoId(Long bookInfoId) {
        return bookInfoMapper.deleteByBookInfoId(bookInfoId);
    }

    @Override
    public List<BookInfo> search(String searchWord) {
        if (StringUtils.isEmpty(searchWord)) {
            searchWord = "";
        }
        searchWord = "%" + searchWord.trim() + "%";
        return bookInfoMapper.findByBookInfoNameOrBookInfoAuthor(searchWord);
    }

    @Override
    public void edit(BookInfo bookInfo) {
        bookInfoMapper.update(bookInfo);
    }

    @Override
    public Map<String, Object> findByBookInfoId(Long bookInfoId) {
        BookInfo bookInfo = bookInfoMapper.findClassInfoAndBookInfoByBookInfoId(bookInfoId);
        List<ClassInfo> classInfoList = classInfoMapper.findAll();
        Map<String,Object> map = new HashMap<>();
        map.put("bookInfo", bookInfo);
        map.put("classInfoList",classInfoList);
        return map;
    }

    @Override
    public void add(BookInfo bookInfo) {
        bookInfoMapper.add(bookInfo);
    }
}
